﻿#include "serversettingsdialog.h"

BEGIN_EVENT_TABLE(ServerSettingsDialog, wxDialog)
    EVT_BUTTON(wxID_OK, ServerSettingsDialog::OnOK)
END_EVENT_TABLE()

ServerSettingsDialog::ServerSettingsDialog(wxWindow* parent, const wxString& title)
    : wxDialog(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize,
               wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
    // Create static boxes
    wxStaticBox* signalBox = new wxStaticBox(this, wxID_ANY, wxT("信号服务器配置"));

    // Create sizers for the static boxes
    wxStaticBoxSizer* signalSizer = new wxStaticBoxSizer(signalBox, wxVERTICAL);

    // Signal server controls
    wxFlexGridSizer* signalGrid = new wxFlexGridSizer(2, 5, 5);
    signalGrid->AddGrowableCol(1);

    wxStaticText* signalIpLabel = new wxStaticText(this, wxID_ANY, wxT("IP地址:"));
    m_signalIpTextCtrl = new wxTextCtrl(this, wxID_ANY, wxT("127.0.0.1"));

    wxStaticText* signalPortLabel = new wxStaticText(this, wxID_ANY, wxT("端口号:"));
    m_signalPortTextCtrl = new wxTextCtrl(this, wxID_ANY, wxT("8888"));

    signalGrid->Add(signalIpLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
    signalGrid->Add(m_signalIpTextCtrl, 1, wxEXPAND | wxALL, 5);
    signalGrid->Add(signalPortLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
    signalGrid->Add(m_signalPortTextCtrl, 1, wxEXPAND | wxALL, 5);

    signalSizer->Add(signalGrid, 1, wxEXPAND | wxALL, 5);

     // Set port number validation
    wxIntegerValidator<int> portValidator;
    portValidator.SetRange(1, 65535);
    m_signalPortTextCtrl->SetValidator(portValidator);

    // Create buttons
    wxButton* okButton = new wxButton(this, wxID_OK, wxT("确定"));
    wxButton* cancelButton = new wxButton(this, wxID_CANCEL, wxT("取消"));

    // Main layout
    wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
    mainSizer->Add(signalSizer, 0, wxEXPAND | wxALL, 10);

    // Button sizer
    wxStdDialogButtonSizer* buttonSizer = new wxStdDialogButtonSizer();
    buttonSizer->AddButton(okButton);
    buttonSizer->AddButton(cancelButton);
    buttonSizer->Realize();
    mainSizer->Add(buttonSizer, 0, wxALIGN_CENTER | wxBOTTOM, 10);

    SetSizerAndFit(mainSizer);
    SetMinSize(wxSize(400, -1));
    okButton->SetDefault();
    CenterOnScreen();
}

ServerSettingsDialog::~ServerSettingsDialog()
{

}

// 设置信号服务器参数
void ServerSettingsDialog::SetSignalServerParams(wxString ip,int port)
{
    if(ip == "" || port <= 0) return;

    m_signalIpTextCtrl->SetLabel(ip);
    m_signalPortTextCtrl->SetLabel(wxString::Format("%d",port));
}

// 获取信号服务器端口号
int ServerSettingsDialog::GetSignalServerPort() const
{
    long port;
    m_signalPortTextCtrl->GetValue().ToLong(&port);
    return static_cast<int>(port);
}

bool ServerSettingsDialog::ValidateFields()
{
    wxString signalIP = m_signalIpTextCtrl->GetValue().Trim();
    wxString signalPort = m_signalPortTextCtrl->GetValue().Trim();

    if (signalIP.IsEmpty()) {
        wxMessageBox(wxT("信号服务器IP地址不能为空!"), wxT("错误"), wxOK | wxICON_ERROR, this);
        m_signalIpTextCtrl->SetFocus();
        return false;
    }

    if (signalPort.IsEmpty()) {
        wxMessageBox(wxT("信号服务器端口号不能为空!"), wxT("错误"), wxOK | wxICON_ERROR, this);
        m_signalPortTextCtrl->SetFocus();
        return false;
    }

    // Additional validation if needed (e.g., IP format, port range)
    long signalport;
    if (!signalPort.ToLong(&signalport) || signalport < 1 || signalport > 65535) {
        wxMessageBox(wxT("信号服务器端口号无效 (1-65535)!"), wxT("错误"), wxOK | wxICON_ERROR, this);
        m_signalPortTextCtrl->SetFocus();
        return false;
    }

    return true;
}

void ServerSettingsDialog::OnOK(wxCommandEvent& event)
{
    if (ValidateFields()) {
        // Only close the dialog if validation passes
        EndModal(wxID_OK);
    }
    // else: don't close the dialog, let user fix the errors
}
